<template>
  <div class="hello">
    <div>===================这里是组件1 start==================</div>
    组件2状态机===a==={{ key2 }}
    <button @click="handleAdd">更新key1</button>
    <div>===================这里是组件1 end==================</div>
  </div>
</template>

<script>
import { createStore } from "@/utils/SharedState";
const { store, subscribe } = createStore();
export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      key2: [],
    };
  },
  created() {},
  mounted() {
    this.$Store.subscribe("key2", (newValue) => {
      this.key2 = newValue;
      // console.log(`这里是组件1,key2新值=`, newValue);
    });
    this.$Store.subscribe("key3", (newValue) => {
      console.log(`这里是组件1,key3新值=`, newValue);
    });
    this.$Store.subscribe("funKey", (newValue) => {
      console.log(`这里是组件1,funKey新值=`, newValue);
    });
    this.key2 = this.$Store.store.key2;
  },
  methods: {
    handleAdd() {
      // state.key1 += 1;
      const arr = this.$Store.store.key1;
      arr.push(`${Date.now()}`);
      this.$Store.dispatch("key1", arr);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
